package com.haiheng.library_base.utils;


import android.annotation.SuppressLint;
import android.text.TextUtils;

import com.haiheng.library.log.LogUtils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期工具类
 *
 * @author songjiali
 * @version 1.0.0
 * @filename DateUtils.java
 * @time 2018/10/22 17:25
 * @copyright(C) 2018 深圳市海恒智能科技有限公司
 */
public class DateUtils {
    private static SimpleDateFormat msFormat = new SimpleDateFormat("mm:ss");

    /**
     * 指定日期格式 yyyyMMddHHmmss
     */
    public static final String DATE_FORMAT_1 = "yyyyMMddHHmmss";

    /**
     * 指定日期格式 yyyy-MM-dd HH:mm:ss
     */
    public static final String DATE_FORMAT_2 = "yyyy-MM-dd HH:mm:ss";

    /**
     * 指定日期格式yyyy-MM-dd HH:mm
     */
    public static final String DATE_FORMAT_3 = "yyyy-MM-dd HH:mm";

    /**
     * 指定日期格式 yyyy-MM-dd
     */
    public static final String DATE_FORMAT_4 = "yyyy-MM-dd";

    /**
     * 指定日期格式 yyyy-MM-dd HH:mm:ss:SSS
     */
    public static final String DATE_FORMAT_5 = "yyyy-MM-dd HH:mm:ss:SSS";


    /**
     * 指定日期格式 yyMMdd,如180619
     */
    public static final String DATE_FORMAT_6 = "yyMMdd";

    /**
     * 指定日期格式 HHmmss,如113038
     */
    public static final String DATE_FORMAT_7 = "HHmmss";

    /**
     * 指定日期格式 yyMMdd,如20180619
     */
    public static final String DATE_FORMAT_8 = "yyyyMMdd";
    public static final String DATE_FORMAT_9 = "YYYYMMddHH";

    /**
     * date转String
     *
     * @param date   日期date
     * @param format 日期格式
     *               比如"yyyy-MM-dd HH:mm:ss"
     * @return
     */
    public static String formatDateTime(Date date, String format) {
        String dateStr = "";
        DateFormat sdf = new SimpleDateFormat(format);
        dateStr = sdf.format(date);
         return dateStr;
    }


    /**
     * String转date
     *
     * @param str    字符串日期
     * @param format 如"yyyy-MM-dd HH:mm:ss"
     * @return
     */
    public static Date stringToDate(String str, String format) {
        DateFormat sdf = new SimpleDateFormat(format);
        Date date = null;
        try {
            date = sdf.parse(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return date;
    }


    /**
     * 把字符时间按指定格式返回
     *
     * @param str    字符串日期
     * @param format 日期格式
     * @return
     */
    public static String formatTime(String str, String format) {
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        Date d = null;
        String date = "";
        if (TextUtils.isEmpty(str)) return "";
        try {
            d = formatter.parse(str);
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            date = sdf.format(d);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 把字符时间按指定格式返回
     *
     * @param str          字符串日期
     * @param sourceFormat 原始日期格式
     * @param targetFormat 目标日期格式
     * @return
     */
    public static String formatTime(String str, String sourceFormat, String targetFormat) {
        SimpleDateFormat formatter = new SimpleDateFormat(sourceFormat);
        Date d = null;
        String date = "";
        try {
            d = formatter.parse(str);
            SimpleDateFormat sdf = new SimpleDateFormat(targetFormat);
            date = sdf.format(d);
        } catch (Exception e) {
            return str;
        }
        return date;
    }


    /**
     * 两个日期相差几天 （天数=endTime - startTime）
     *
     * @param startTime
     * @param endTime
     * @return
     */
    public static long dateDiff(Date startTime, Date endTime) {
        long nd = 1000 * 24 * 60 * 60;// 一天的毫秒数
        long nh = 1000 * 60 * 60;// 一小时的毫秒数
        long nm = 1000 * 60;// 一分钟的毫秒数
        long ns = 1000;// 一秒钟的毫秒数
        long diff;
        long day = 0;
        // 获得两个时间的毫秒时间差异
        diff = endTime.getTime()
                - startTime.getTime();
        day = diff / nd;// 计算差多少天
        long hour = diff % nd / nh;// 计算差多少小时
        long min = diff % nd % nh / nm;// 计算差多少分钟
        long sec = diff % nd % nh % nm / ns;// 计算差多少秒
        // 输出结果
        LogUtils.i("时间相差：" + day + "天" + hour + "小时" + min
                + "分钟" + sec + "秒。");
        if (day >= 1) {
            return day;
        } else {
            if (day == 0) {
                return 1;
            } else {
                return 0;
            }

        }
    }

    /**
     * 和当前日期进行比较
     *
     * @param date
     * @param format
     * @return true已经超出当前日期，false没有超出当前日期
     */
    public static boolean compareCurrentDate(String date, String format) {
        DateFormat df = new SimpleDateFormat(format);
        try {
            Date dt1 = df.parse(date);
            Date dt2 = new Date();
            if (dt1.getTime() >= dt2.getTime()) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }


    /**
     * date转换成字符串
     *
     * @param destFormat
     * @param date
     * @return
     */
    public static String dateToString(String destFormat, Date date) {
        String strDate = null;
        if (date != null) {
            SimpleDateFormat format = new SimpleDateFormat(destFormat);
            strDate = format.format(date);
        }
        return strDate;
    }


    /**
     * MS turn every minute
     *
     * @param duration Millisecond
     * @return Every minute
     */
    public static String timeParse(long duration) {
        String time = "";
        if (duration > 1000) {
            time = timeParseMinute(duration);
        } else {
            long minute = duration / 60000;
            long seconds = duration % 60000;
            long second = Math.round((float) seconds / 1000);
            if (minute < 10) {
                time += "0";
            }
            time += minute + ":";
            if (second < 10) {
                time += "0";
            }
            time += second;
        }
        return time;
    }

    /**
     * MS turn every minute
     *
     * @param duration Millisecond
     * @return Every minute
     */
    public static String timeParseMinute(long duration) {
        try {
            return msFormat.format(duration);
        } catch (Exception e) {
            e.printStackTrace();
            return "0:00";
        }
    }

    public static long getMothonZero(){
        Date date = new Date();
        long l = 24*60*60*1000; //每天的毫秒数
        return (date.getTime() - (date.getTime()%l) - 8* 60 * 60 *1000);
    }

    public static long getTodayZero() {
        Date date = new Date();
        long l = 24*60*60*1000; //每天的毫秒数
        //date.getTime()是现在的毫秒数，它 减去 当天零点到现在的毫秒数（ 现在的毫秒数%一天总的毫秒数，取余。），理论上等于零点的毫秒数，不过这个毫秒数是UTC+0时区的。
        //减8个小时的毫秒值是为了解决时区的问题。
        return (date.getTime() - (date.getTime()%l) - 8* 60 * 60 *1000);
    }

    @SuppressLint("SimpleDateFormat")
    public static String getTime(long time) {
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d1=new Date(time);
        return format.format(d1);
    }

    public static long getCurrentMothZero(){
        try {
            SimpleDateFormat curentMoth = new SimpleDateFormat("yyyy-MM");
            return curentMoth.parse(curentMoth.format(new Date())).getTime();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 获取当天时间
     * @return
     */
    public static long todaySystemDate() {
        try {
            SimpleDateFormat simpleformat = new SimpleDateFormat("yyyy-MM-dd");
            SimpleDateFormat curentMoth = new SimpleDateFormat("yyyy-MM");
            return simpleformat.parse(simpleformat.format(new Date())).getTime();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;

    }
    public static Integer getCurrentDayOfWeek() {
        Calendar calendar = Calendar.getInstance();
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);

        switch (dayOfWeek) {
            case Calendar.SUNDAY:
                return 7;
            case Calendar.MONDAY:
                return 1;
            case Calendar.TUESDAY:
                return 2;
            case Calendar.WEDNESDAY:
                return 3;
            case Calendar.THURSDAY:
                return 4;
            case Calendar.FRIDAY:
                return 5;
            case Calendar.SATURDAY:
                return 6;
            default:
                return 0;
        }
    }
}
